from wxpyview.ui.component.common.basewidget import *

class MDIChildFrame(Widget):
    def _getParentParam(self, meta_widget):
        stopping_point = ['MDIParentFrame']       
        return ComponentLibrary.findFirstMatchedAncestor(meta_widget, stopping_point)
    
    def _setParams(self, meta_widget):
        dom = meta_widget.getDOM()
        if not dom:
            raise IOException
        
        self.params["parent"] = self._getParentParam(meta_widget)
        self.params["id"] = -1
        self.params["title"] = dom.getAttribute("title")
        self.params["pos"] = ComponentLibrary.getPosParam(dom)
        self.params["size"] = ComponentLibrary.getSizeParam(dom)      
        #self.params["style"]
        self.params["name"] = dom.getAttribute("name")

        self.minWidth = ComponentLibrary.str2int(dom.getAttribute("minWidth"), -1)
        self.minHeight = ComponentLibrary.str2int(dom.getAttribute("minHeight"), -1)
    
    def create(self, meta_widget):
        self._setParams(meta_widget)

        params = self.params
        
        # template class for creating wx.MDIChildFrame on the fly
        class MDIChildFrame(wx.MDIChildFrame):
            create_index = 0
            
            def __init__(self):
                MDIChildFrame.create_index += 1
                params["name"] += str(MDIChildFrame.create_index)
                
                wx.MDIChildFrame.__init__(self, **params)
                
                print MDIChildFrame.create_index
                
        frame_obj = MDIChildFrame()
        
        return frame_obj